package cn.tedu._05weibo.intercepters;

import cn.tedu._05weibo.pojo.vo.userVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Slf4j
public class UserLoginIntercepter implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//        登录状态效验: 使用拦截器方式实现
        HttpSession session = request.getSession();
        userVO  userVO= (cn.tedu._05weibo.pojo.vo.userVO) session.getAttribute("user");
        if (userVO == null){ //没有用户登录
            response.sendRedirect("/login.html");
            return false;
        }
        log.debug("通过拦截器对登录功能的效验");
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
